package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.gaokao.com/ 高考网
 *  abc
 */
public class GaokaoNewsAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://www.gaokao.com/e/[0-9]{8}/[0-9a-z]*.shtml"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
//		String source="高考网";


		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select(".content h1").text();
		dateStr=doc.select(".tip_b").text();
		content=HtmlCleaner.getContentHtml(url,doc.select(".content_txt p"));
		if(StringUtils.isBlank(content))
			content=HtmlCleaner.getContentHtml(url,doc.select(".main p"));
		if(title==null || title.trim().length()==0){
		}
		date=DateUtils.matchDate(dateStr);
		/**
		 * 初始化、截取 、设置作者信息*******************************************
		 *
		 * */
		String str=null;
		String author=null;
		author=doc.select(".tip_b span").eq(1).text();
//		author=StringUtils.substringBetween(str,"来源：","编辑：");
		news.setAuthor(author);
		/**
		 *                        	 **************************end *************************
		 *
		 **/

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		/**
		 * 解析用于更新的地址
		 */
		String updateUrl=null;

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		GaokaoNewsAnalyse a=new GaokaoNewsAnalyse();
		String url="http://www.gaokao.com/e/20130619/51c1584571cb4.shtml";
		String fromatUrl=UrlArgumentTop.FromatUrl(url);
		System.out.println("url="+fromatUrl);
		System.out.println(a.isDetailPage(fromatUrl));
		UrlMeta meta=CrawlHTML.responseToURL(fromatUrl);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate() {
		return false;
	}
}
